<?php
/**
 * 商品评价
 */

namespace app\api\model;


use app\common\model\Model;
use think\db\Query;

class GoodsComment extends Model
{
    const TABLE_NAME = 'goods_comment';
    const CACHE_EXPIRE_LEVEL = self::CACHE_EXPIRE_LEVEL_4;

    const COMMENT_IMAGE_SAVE_PATH='goods_comment';
    protected $name = self::TABLE_NAME;

    public static function addComment($user_id,$comment_text,$stars,$images,$order_id,$goods_id=0)
    {
        $image_names = '';
        if($images)
        {
            $image_names=implode(',',$images);
        }
        return self::commonQuery(self::TABLE_NAME)
              ->insert([
                  'user_id'=>$user_id,
                  'order_id'=>$order_id,
                  'goods_id'=>$goods_id,
                  'comment_text'=>$comment_text,
                  'comment_time'=>time(),
                  'stars'=>$stars,
                  'comment_imgs'=>$image_names
              ]);
    }
    public static function getCommentInfo($comment_id)
    {
        return self::commonQuery(self::TABLE_NAME)
               ->where(['comment_id'=>$comment_id])
               ->find();
    }
    public static function getComment($goods_id,$page=1,$limit=self::LIMIT)
    {
        return self::commonQuery(self::TABLE_NAME)
               ->where(['goods_id'=>$goods_id])
               ->order('comment_time desc')
               ->page($page,$limit)
               ->select();
    }
    //获取 商品评价 和 用户评价
    public static function handleCommentList($list,$get_goods_info=false)
    {
        $res =[];
        if(empty($list))
        {
            return $res;
        }
        foreach ($list as $key=>$val)
        {
            $res[$key]=User::handleUserCommonInfo($val['user_id']);
            $res[$key]['comment_id']=$val['comment_id'];
            $res[$key]['comment_text']=htmlspecialchars_decode($val['comment_text']);
            $res[$key]['comment_time']=date('Y-m-d H:i',$val['comment_time']);
            $res[$key]['stars']=$val['stars'];
            $res[$key]['comment_imgs']=[];
            if($val['comment_imgs'])
            {
                $images = explode(',',$val['comment_imgs']);
                foreach ($images as $v)
                {
                    $res[$key]['comment_imgs'][]=get_image_url(self::COMMENT_IMAGE_SAVE_PATH,$v);
                }
            }

            if($get_goods_info)
            {
                $goods_info = Goods::getGoodsInfo($val['goods_id']);
                $res[$key]['goods_name']=$goods_info['goods_name'];
            }
            
        }
        return $res;
    }
    public static function deleteComment($comment_id,$user_id)
    {
        return self::commonQuery(self::TABLE_NAME)
               ->where(['comment_id'=>$comment_id,'user_id'=>$user_id])
               ->delete();
    }

    //获取用户评价
    public static function getUserCommentList($user_id,$page=1,$limit=self::LIMIT)
    {
        return self::commonQuery(self::TABLE_NAME)
            ->where(['user_id'=>$user_id])
            ->order('comment_time desc')
            ->page($page,$limit)
            ->select();
    }


}